package com.lwbldy.modules.system.controller;

import com.github.pagehelper.PageHelper;
import com.lwbldy.common.shiro.ShiroUtils;
import com.lwbldy.common.tools.PageUtils;
import com.lwbldy.common.tools.R;
import com.lwbldy.common.tools.StringUtils;
import com.lwbldy.modules.system.domain.SysRole;
import com.lwbldy.modules.system.domain.SysUser;
import com.lwbldy.modules.system.service.SysRoleService;
import com.lwbldy.modules.system.service.SysUserService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统用户
 * @author lwbldy
 * @email lwbldy@163.com
 * @date 2019-03-27 11:03:56
 */
@Controller
@RequestMapping("system/user")
public class SysUserController {

    /**
     * Describe: 基础路径
     * */
    private static String MODULE_PATH = "system/user/";

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    @ResponseBody
    public R info(@PathVariable("userId") Long userId){
        SysUser sysUser = sysUserService.queryById(userId);
        return R.ok().put("sysUser", sysUser);
    }

    @GetMapping("/main")
    @RequiresPermissions({"system:user:main"})
    public String list(){
        return MODULE_PATH + "main";
    }

    @ApiOperation(value = "根据用户名分页查询用户")
    @PostMapping("/list")
    @ResponseBody
    @RequiresPermissions({"system:user:main"})
    public R list(@RequestParam(value = "userName", defaultValue = "") String userName,
                  @RequestParam(value = "page", defaultValue = "1") Integer page,
                  @RequestParam(value = "limit", defaultValue = "15") Integer limit){
        PageHelper.startPage(page, limit);
        Map<String,Object> primaryMap = new HashMap<>();
        primaryMap.put("userName",userName);
        List<SysUser> brandList = sysUserService.queryList(primaryMap);
        return PageUtils.restPage(brandList);
    }


    /**
     * 获取用户新增视图
     * @return
     */
    @GetMapping("add")
    @RequiresPermissions({"system:user:save"})
    public String add(Model model){
        model.addAttribute("sysRoles",sysRoleService.listAll());
        return MODULE_PATH + "add";
    }

    /**
     * 保存
     * @param sysUser
     * @return
     */
    @PostMapping("save")
    @ResponseBody
    @RequiresPermissions({"system:user:save"})
    public R save(@RequestBody SysUser sysUser){
        return R.resultRow(sysUserService.save(sysUser));
    }

    /**
     * 获取用户修改视图
     * @param model
     * @param id
     * @return
     */
    @GetMapping("edit")
    @RequiresPermissions({"system:user:edit"})
    public String edit(ModelMap model, long id){

        model.addAttribute("sysRoles",sysRoleService.listAll());
        model.addAttribute("sysUser",sysUserService.queryById(id));
        return MODULE_PATH + "edit";
    }

    /**
     * 修改用户信息
     * @param sysUser
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    @RequiresPermissions({"system:user:edit"})
    public R update(@RequestBody SysUser sysUser){
        return R.resultRow(sysUserService.updateUserAndRole(sysUser));
    }


    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @DeleteMapping("remove/{id}")
    @ResponseBody
    @RequiresPermissions({"system:user:remove"})
    public R remove(@PathVariable long id){
        int result = sysUserService.remove(id);
        return R.resultRow(result);
    }

    @DeleteMapping("removeBatch/{ids}")
    @ApiOperation(value="批量删除用户数据")
    @ResponseBody
    @RequiresPermissions({"system:user:remove"})
    public R removeBatch(@PathVariable String ids){
        int result = sysUserService.removeBatch(StringUtils.StringIdsToArr(ids));
        return R.resultRow(result);
    }

    /**
     * Describe: 根据 Id 启用用户
     * Param: roleId
     * Return: ResuBean
     * */
    @PutMapping("enable")
    @ApiOperation(value="启用用户")
    public R enable(@RequestBody SysUser sysUser){
        sysUser.setStatus(1);
        return R.resultRow(sysUserService.update(sysUser));
    }

    /**
     * Describe: 根据 Id 禁用用户
     * Param: roleId
     * Return: ResuBean
     * */
    @PutMapping("disable")
    @ApiOperation(value="禁用用户")
    public R disable(@RequestBody SysUser sysUser){
        sysUser.setStatus(0);
        return R.resultRow(sysUserService.update(sysUser));
    }

    @PutMapping("adminChangePWD")
    @ApiOperation(value="修改密码")
    @ResponseBody
    @RequiresPermissions({"system:user:changePWD"})
    public R adminChangePWD(long userId,String pwd){
        return R.resultRow(sysUserService.changePWD(userId,pwd));
    }

    @PutMapping("changePWD")
    @ApiOperation(value="修改密码")
    @ResponseBody
    public R changePWD(String pwd){
        return R.resultRow(sysUserService.changePWD(ShiroUtils.getUserId(),pwd));
    }

}
